﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Demo2
{
    class Program
    {
        static void Main(string[] args)
        {
            Student student = new Student("001","wyy",20);
            Student student1 = new Student("003", "eee",18);
            Student student2 = new Student("004", "vvv",23);
            //定义一个哈希表的泛型
            Dictionary<string, Student> dc = new Dictionary<string, Student>();
            //把这些已经手动存入的信息 存入泛型中
            dc.Add(student.Id, student);
            dc.Add(student1.Id, student1);
            dc.Add(student2.Id, student2);
            while (true)
        {
            Console.WriteLine("1、添加学生信息。2、删除学生信息 3、查询学生信息。");
            string chooes = Console.ReadLine();
            switch (chooes)
            {
                case "1":
                    AddInfo(dc);
                    Print(dc);
                    break;
                case "2":
                    DeleteInfo(dc);
                    Print(dc);
                    break;
                case "3":
                    SelectInfo(dc);
                    break;
                default:
                    break;
            }
        }
        }
        private static void SelectInfo(Dictionary<string, Student> dc)
        {
            bool flag = true;
            while (flag)
            {
            Console.WriteLine("请选择你需要的模块：1、查询所有（按学号排序）2、查询所有（按姓名排序），3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
            string chooes = Console.ReadLine();
            switch (chooes)
            {
                case "1":
                    SelectById(dc);
                    break;
                case "2":
                    SelectByName(dc);
                    break;
                case "3":
                    SelectByAge(dc);
                    break;
                case "4":
                    SelectInfoById(dc);
                    break;
                case "5":
                    flag = false;
                    break;
                default:
                    break;
            }
            }
           
        }

        private static void SelectInfoById(Dictionary<string, Student> dc)
        {

            while (true)
            {
            Console.WriteLine("请输入你要查询的学生ID");
            string Id = Console.ReadLine();
            if (dc.ContainsKey(Id))
            {
                Console.WriteLine(dc[Id]);
                
            }
            else
            {
                Console.WriteLine("查无此人！");  
            }
            }
            
        }

        private static void SelectByAge(Dictionary<string, Student> dc)
        {
            List<Student> list = new List<Student>();
            list.AddRange(dc.Values);
            list.Sort(new SortAge());
            PrintBeforeSort(list);
        }

        private static void SelectByName(Dictionary<string, Student> dc)
        {
            List<Student> list = new List<Student>();
            list.AddRange(dc.Values);
            list.Sort(new SortName());
            PrintBeforeSort(list);
        }

        private static void SelectById(Dictionary<string, Student> dc)
        {
            List<Student> list = new List<Student>();
            //因为dictionary是无序的 需要把无序的放进list里面进行有序的排序
            list.AddRange(dc.Values);
            list.Sort(new SortId());
            PrintBeforeSort(list);
        }

        private static void PrintBeforeSort(List<Student> list)
        {
            foreach (var item in list)
            {
                Console.WriteLine(item);
            }
        }

        private static void DeleteInfo(Dictionary<string, Student> dc)
        {
            Console.WriteLine("目前拥有的信息：");
            foreach (var item in dc.Values)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("请输入要删除的学号：");
            string Id = Console.ReadLine();
            if (dc.ContainsKey(Id))
            {

                dc.Remove(Id);
            }
            else
            {
                Console.WriteLine("找不到此学生");
            }
        }

        private static void Print(Dictionary<string, Student> dc)
        {
            foreach (var item in dc.Values)
            {
                Console.WriteLine(item);
            }
        }

        private static void AddInfo(Dictionary<string, Student> dc)
        {
            string flag = null;
            while (true)
            {

            
            Console.WriteLine("请输入要添加的学号：");
            string Id = Console.ReadLine();
            Console.WriteLine("请输入要添加的学生姓名：");
            string name = Console.ReadLine();
            Console.WriteLine("请输入要添加的学生年龄：");
            int age = int.Parse(Console.ReadLine());
            //实例化学生对象
            Student student = new Student(Id, name, age);
            if (dc.ContainsKey(Id))
            {
                Console.WriteLine("输入的学号已存在！！请重新输入！");
                    continue;
            }
            else
            {
                dc.Add(student.Id,student);
                Console.WriteLine("添加成功");
            }
                Console.WriteLine("是否继续添加：（请输入YorN）");
                 flag = Console.ReadLine();
                if (!flag.Equals("Y")&&!flag.Equals("y"))
                {
                    break;
                }
                

            }
        }
    }
}
